<?php
/**
 * 
 *
 * @author 
 * @version v1.0.0
 *
 **/
namespace Home\Controller;
use Think\Controller;

class RoomController extends Controller {
	public function __construct() {
        parent::__construct();
		//判断是否登陆
		if(session('user_id')){ //已登陆
			
		}else{ //未登陆，跳转道登陆界面
			$this->redirect("index/index");
		}
    }
	
	public function index(){
		if(IS_GET){
			$room_id = I('room_id') ? (int)I('room_id') : 0;
			if($room_id){
				$info = D('Room')->getRoomInfoById($room_id);
				if(is_array($info) && count($info)){
					$result = array('errCode' => 1, 'info' => $info);
				}else{
					$result = array('errCode' => 0);
				}
				$this->ajaxReturn($result, 'json');
			}else{
				$page = I('get.page') ? (int)I('get.page') : 1;
				$ajax = I('get.ajax') ? (int)I('get.ajax') : 0;
				$count = D('Room')->getRoomListCount();
				//用户列表
				$list = D('Room')->getRoomList($page);
				
				$this->assign('list', $list);
				$this->assign('count', $count);
				$this->assign('page', $page);
				//判断请求方式
				if(!$ajax){
					$this->display('index');
				}else{
					$this->display('room_list', 'utf-8', 'html');
				}
			}
		}else if(IS_POST){
			$data = array(
				'room_number' => I('post.room_number') ? trim(I('post.room_number')) : '',
				'createtime' => time()
			);
			
			$room_id = I('post.room_id') ? (int)I('post.room_id') : 0;
			if(empty($data['room_number'])){
				$this->ajaxReturn('请输入房间号');
			}else{
				if($room_id){
					$row = D('Room')->editRoomInfo($room_id, $data);
				}else{
					$info = D('Room')->getRoomInfoByNumber($data['room_number']);
					if(is_array($info) && count($info)){
						$this->ajaxReturn('房间号已存在');
						exit;
					}else{
						$row = D('Room')->addRoomInfo($data);
					}
				}
				
				if($row !== false){
					$this->ajaxReturn('操作成功');
				}else{
					$this->ajaxReturn('操作失败');
				}
			}
		}
	}
	
	/**
	 * 删除
	 **/
	public function remove(){
		if(IS_POST){
			$id = I('post.id') ? (int)I('post.id') : 0;
			$row = D('Room')->delRoomInfo($id);
			if($row !== false){
				$this->ajaxReturn(1, 'json');
			}else{
				$this->ajaxReturn(0, 'json');
			}
		}
	}
}


   